package com.example.demo.service.imply;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.exception.MyUncheckedException;
import com.example.demo.mapper.*;
import com.example.demo.pojo.dto.QuestionDTO;
import com.example.demo.pojo.entity.QuestionBank;
import com.example.demo.pojo.entity.QuestionFile;
import com.example.demo.pojo.entity.QuestionLabel;
import com.example.demo.pojo.entity.SubjectFile;
import com.example.demo.pojo.store.BankStore;
import com.example.demo.service.IQuestionFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fjh
 */
@Service
@RequiredArgsConstructor
public class IQuestionFIleServiceImply  extends ServiceImpl<QuestionFileMapper, QuestionFile> implements IQuestionFileService {
    private final QuestionBankMapper questionBankMapper;
    private final QuestionClassificationMapper questionClassificationMapper;
    private final QuestionLabelMapper questionLabelMapper;
    private final QuestionCategoryMapper questionCategoryMapper;
    private final BankStore bankStore;
    private final QuestionFileMapper questionFileMapper;
    private final SubjectFileMapper subjectFileMapper;
    //根据传入的数据构建出用于更新插入的题目
    QuestionBank createQuestionBank(QuestionDTO questionDTO){
        QuestionBank questionBank=new QuestionBank();
        questionBank.setName(questionDTO.getName());
        questionBank.setCorrectAnswer(questionDTO.getCorrectAnswer());
        questionBank.setScore(questionDTO.getScore());
        questionBank.setCategory(questionDTO.getCategory());
        if(questionDTO.getCategory()!=null){
            questionBank.setDifficulty(questionDTO.getDifficulty());
        }
        questionBank.setParse(questionDTO.getParse());
        questionBank.setHtmlName(questionDTO.getHtmlName());
        questionBank.setHtmlParse(questionDTO.getHtmlParse());
        //对复杂数据进行处理
        List<String>options=questionDTO.getOptions();
        List<String>htmlOptions=questionDTO.getHtmlOptions();
        StringBuilder option= new StringBuilder();
        StringBuilder htmlOption=new StringBuilder();
        StringBuilder label=new StringBuilder();
        List<Integer>labelIds=questionDTO.getLabelIds();
        //处理选项
        if(options!=null&&htmlOptions!=null){
            for(int i=0;i<options.size();i++){
                String oneOption=options.get(i);
                String oneHtmlOption=htmlOptions.get(i);
                option.append(oneOption);
                option.append('|');
                htmlOption.append(oneHtmlOption);
                htmlOption.append('|');
            }
            questionBank.setOptions(String.valueOf(option));
            questionBank.setHtmlOption(String.valueOf(htmlOption));
        }
        //处理题型
        if(labelIds!=null) {
            for (int i = 0; i < labelIds.size(); i++) {
                QuestionLabel questionLabel = questionLabelMapper.selectById(labelIds.get(i));
                if(questionLabel!=null){
                    label.append(questionLabel.getName());
                    label.append(';');
                }
            }
            questionBank.setLabel(String.valueOf(label));
        }
        return questionBank;
    }
    //构建题目
    @Override
    public int createQuestion(QuestionDTO questionDTO) {
        if(questionDTO!=null){
            //获取基本信息
            QuestionBank questionBank=createQuestionBank(questionDTO);
            questionBankMapper.insert(questionBank);
            int questionBankId=questionBank.getId();
            SubjectFile subjectFile=subjectFileMapper.selectById(questionDTO.getFileId());
            if(subjectFile!=null){
                QuestionFile questionFile=new QuestionFile();
                questionFile.setQuestionId(questionBankId);
                questionFile.setNodeId(subjectFile.getNodeId());
                questionFile.setFileId(subjectFile.getId());
                questionFileMapper.insert(questionFile);
            }
            return questionBank.getId();
        }
        return -1;
    }
    @Override
    public QuestionFile shiftQuestion(int questionId,int presentFileId,int goalFileId) throws MyUncheckedException {
        int queryFileId=presentFileId;
        int queryQuestionId=questionId;
        QueryWrapper<QuestionFile> queryWrapperPresent=new QueryWrapper<>();
        queryWrapperPresent.eq("file_id",queryFileId);
        queryWrapperPresent.eq("question_id",queryQuestionId);
        List<QuestionFile> questionFileList=questionFileMapper.selectList(queryWrapperPresent);
        System.out.println(queryQuestionId+"-"+presentFileId+"+"+goalFileId);
        if(questionFileList!=null) {
            QuestionFile questionFile=questionFileList.get(0);
            UpdateWrapper<QuestionFile> updateWrapper=new UpdateWrapper<>();
            updateWrapper.eq("file_id",questionFile.getFileId());
            updateWrapper.eq("question_id",questionId);
            //去判断新目录下是否存在这个题目
            questionFile.setFileId(goalFileId);
            queryWrapperPresent.eq("question_id",queryQuestionId);
            queryWrapperPresent.eq("file_id",goalFileId);
            if(questionFileMapper.selectList(queryWrapperPresent).isEmpty()){
                questionFileMapper.update(questionFile,updateWrapper);
                return questionFile;
            }
            throw new MyUncheckedException("目录下以及存在了这个题目");
        }
        throw new MyUncheckedException("没有查找到");
    }

    @Override
    public QuestionFile copyQuestion(int questionId, int presentFileId, int goalFileId) throws MyUncheckedException{
        int queryFileId=presentFileId;
        int queryQuestionId=questionId;
        QueryWrapper<QuestionFile>queryWrapperPresent=new QueryWrapper<>();
        queryWrapperPresent.eq("file_id",queryFileId);
        queryWrapperPresent.eq("question_id",queryQuestionId);
        List<QuestionFile> questionFileList=questionFileMapper.selectList(queryWrapperPresent);
        if(questionFileList!=null) {
            System.out.println("进入到");
            QuestionFile questionFile=questionFileList.get(0);
            //去判断新目录下是否存在这个题目
            questionFile.setFileId(goalFileId);
            queryWrapperPresent.eq("question_id",queryQuestionId);
            queryWrapperPresent.eq("file_id",goalFileId);
            if(questionFileMapper.selectList(queryWrapperPresent).isEmpty()){
                QuestionFile insertQuestionFile=new QuestionFile();
                insertQuestionFile.copyQuestionFile(questionFile);
                System.out.println(insertQuestionFile);
                questionFileMapper.insert(insertQuestionFile);
                return insertQuestionFile;
            }
            throw new MyUncheckedException("目录下以及存在了这个题目");
        }
        throw new MyUncheckedException("没有查找到");
    }

    @Override
    public int deleteQuestion(int questionId, int fileId) throws MyUncheckedException {
        QueryWrapper<QuestionFile>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("file_id",fileId);
        queryWrapper.eq("question_id",questionId);
        List<QuestionFile> questionFileList=questionFileMapper.selectList(queryWrapper);
        if(!questionFileList.isEmpty()){
            for(QuestionFile questionFileFor :questionFileList){
                int deleteId=questionFileFor.getId();
                questionFileMapper.deleteById(deleteId);
            }
            return 1;
        }
        throw new MyUncheckedException("出现错误--查找不到题目信息");
    }

    @Override
    public QuestionBank updateQuestion(int questionId, QuestionDTO questionDTO) {
        //建立wrapper
        UpdateWrapper<QuestionBank>updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",questionId);
        //建立更新实体
        QuestionBank questionBank=createQuestionBank(questionDTO);
        //实现更新
        questionBankMapper.update(questionBank,updateWrapper);
        return questionBank;
    }
}
